package com.pureut.storage.controller;


import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.*;
import com.pureut.storage.export.CheckManagementExport;
import com.pureut.storage.service.CheckManagementService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 库存盘点控制层
 *
 * @author: CM
 * @date: 2023/1/13  11:13
 */
@RestController
@RequestMapping("/checkManagement")
public class CheckManagementController extends BaseController {

    @Resource
    private CheckManagementService checkManagementService;

    /**
     * 新增盘点单
     */
    @RequiresPermissions("warehouse:inside:inventoryCount:add")
    @Log(title = "新增盘点单", businessType = BusinessType.INSERT)
    @PostMapping("/addCheck")
    public AjaxResult addCheck(@RequestBody AddCheckVo addCheckVo) throws Exception {
        return toAjax(checkManagementService.addCheck(addCheckVo));
    }

    /**
     * 查看盘点单
     */
    @RequiresPermissions("warehouse:inside:inventoryCount:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(checkManagementService.view(id));
    }

    /**
     * 审核单据
     */
    @RequiresPermissions("warehouseinside:InventoryCount:examine")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PutMapping("/purchaseOrderReview")
    public AjaxResult purchaseOrderReview(@Validated @RequestBody OrderReviewVo orderReviewVo) {
        return toAjax(checkManagementService.checkManagementReview(orderReviewVo));
    }


    /**
     * 删除单据
     */
    @RequiresPermissions("warehouse:inside:inventoryCount:remove")
    @Log(title = "删除单据", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable("ids") String ids) throws Exception {
        return toAjax(checkManagementService.delete(ids));
    }

    /**
     * 关结单据
     */
    @RequiresPermissions("warehouse:inside:inventoryCount:close")
    @Log(title = "关结返工出库单", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public AjaxResult close(@RequestBody CheckCloseVo checkCloseVo) throws Exception {
        return toAjax(checkManagementService.close(checkCloseVo));
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    @RequiresPermissions("warehouse:inside:inventoryCount:close")
    public AjaxResult inspectionMessageExport(HttpServletResponse response, CheckManagementVo checkManagementVo) throws IOException {
        List<CheckManagementExport> infoExport = checkManagementService.getInfoExport(checkManagementVo);
        ExcelUtil<CheckManagementExport> excelUtil = new ExcelUtil<CheckManagementExport>(CheckManagementExport.class);
        excelUtil.excelWriter(response, infoExport, "盘点单信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 盘点信息列表查询
     */
    @GetMapping("/list")
    public TableDataInfo getList(CheckManagementVo checkManagementVo) {
        startPage();
        //查询库存列表
        List<CheckListDto> list = checkManagementService.getList(checkManagementVo);
        return getDataTable(list);
    }

    /**
     * 查看单据明细
     */
    @GetMapping("/detailList")
    public TableDataInfo getListDetail(String docNum) {
        startPage();
        //查询库存列表
        List<CheckDetailListDto> list = checkManagementService.getListDetail(docNum);
        return getDataTable(list);
    }

    /**
     * 查看物料明细
     */
    @GetMapping("/materialList")
    public TableDataInfo getmaterialList(String docNum) {
        startPage();
        //查询库存列表
        List<CheckManagmentMaterialDto> list = checkManagementService.getmaterialList(docNum);
        return getDataTable(list);
    }

    /**
     * 修改盘点信息
     */
    @RequiresPermissions("warehouse:inside:inventoryCount:edit")
    @Log(title = "修改返工出库", businessType = BusinessType.UPDATE)
    @PostMapping("/updateCheck")
    public AjaxResult updateCheck(@RequestBody AddCheckVo addCheckVo) throws Exception {
        return toAjax(checkManagementService.updateCheck(addCheckVo));
    }


    /**
     * 盘点
     */
    @RequiresPermissions("warehouse:inside:inventoryCount:pandian")
    @Log(title = "盘点", businessType = BusinessType.UPDATE)
    @PostMapping("/pdCheck")
    public AjaxResult pdCheck(@RequestBody PdVo pdVo) throws Exception {
        return AjaxResult.success(checkManagementService.pdCheck(pdVo));
    }

    /**
     * 弹窗提示
     */
    @Log(title = "盘点", businessType = BusinessType.UPDATE)
    @PostMapping("/openWindow")
    public AjaxResult openWindow(@RequestBody PdVo pdVo) throws Exception {
        return AjaxResult.success(checkManagementService.openWindow(pdVo));
    }

    /**
     * 盘点完成
     */
    @Log(title = "盘点完成", businessType = BusinessType.UPDATE)
    @PostMapping("/checkInventory")
    public AjaxResult checkInventory(@RequestBody PdVo pdVo) throws Exception {
        return AjaxResult.success(checkManagementService.checkInventory(pdVo));
    }

    /**
     * 盘点物料下拉
     */
    @GetMapping("/getMaterial")
    public AjaxResult getMaterial(Long id) throws Exception {
        return AjaxResult.success(checkManagementService.getMaterial(id));
    }

    /**
     * 盘点单审核-单据明细
     */
    @GetMapping("/reviceDocDetail/{id}")
    public AjaxResult reviceDocDetail(@PathVariable("id") Long id) throws Exception {

        return AjaxResult.success(checkManagementService.reviceDocDetail(id));

    }

    /**
     * 盘点单审核-物料明细
     */
    @GetMapping("/reviceMaterialDetail/{id}")
    public AjaxResult reviceMaterialDetail(@PathVariable("id") Long id) throws Exception {

        return AjaxResult.success(checkManagementService.reviceMaterialDetail(id));


    }

    /**
     * 盘点审核回显
     */
    @GetMapping("/getRevice/{id}")
    public AjaxResult getRevice(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(checkManagementService.getRevice(id));
    }

    /**
     * 审核OK
     */
    @Log(title = "审核Ok", businessType = BusinessType.UPDATE)
    @PutMapping("/reviceOk")
    public AjaxResult reviceOk(@RequestBody PdVo pdVo) throws Exception {
        return toAjax(checkManagementService.reviceOk(pdVo));
    }

    /**
     * 审核NG
     */
    @Log(title = "审核NG", businessType = BusinessType.UPDATE)
    @PutMapping("/reviceNg")
    public AjaxResult reviceNg(@RequestBody PdVo pdVo) throws Exception {
        return toAjax(checkManagementService.reviceNg(pdVo));
    }

    /**
     * 单据审核确定
     */
    @RequiresPermissions("warehouse:inside:InventoryCount:examine")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PutMapping("/checkReview")
    public AjaxResult checkReview(@Validated @RequestBody OrderReviewVo orderReviewVo) throws Exception {
        return toAjax(checkManagementService.checkReview(orderReviewVo));
    }


    /**
     * 盘点物料Pda列表
     */
    @GetMapping("/getCheckListPda")
    public TableDataInfo getCheckListPda() {
        startPage();
        //查询库存列表
        List<CheckListPdaDto> list = checkManagementService.getCheckListPda();
        return getDataTable(list);
    }

    /**
     * 手持Pda明细盘点列表
     */
    @GetMapping("/getCheckDetailListPda")
    public AjaxResult getCheckDetailListPda(CheckListDetailPdaVo checkListDetailPdaVo) {
        //查询库存列表
        CheckListDetailPdaInfoDto checkDetailListPda = checkManagementService.getCheckDetailListPda(checkListDetailPdaVo);
        return AjaxResult.success(checkDetailListPda);
    }


}
